<template>
  <!-- 盘点表 -->
  <p class="pan">
    盘点详情

    <el-button size="small" style="color: #fff" color="#FFD200" @click="add">新增</el-button>
  </p>

  <!-- 新增框 -->
  <el-dialog title="详情新增" v-model="open">
    <el-form :model="form" label-position="right" label-width="100px">
      <div class="vip_info">库存详情:</div>
      <el-form-item label="商品名称:" prop="sp" :required="true">
        <el-select v-model="form.sp" placeholder="请选择">
          <el-option label="红乌苏" value="红乌苏" />
          <el-option label="红乌苏1" value="红乌苏1" />
          <el-option label="红乌苏2" value="红乌苏2" />
          <el-option label="红乌苏3" value="红乌苏3" />
        </el-select>
      </el-form-item>
      <el-form-item label="单位:" prop="dw">
        <el-radio-group v-model="form.dw" text-color="#fff" fill="#ffae00">
          <el-radio-button label="箱" class="radio_btn" />
          <el-radio-button label="瓶" class="radio_btn" />
          <el-radio-button label="盒" class="radio_btn" />
        </el-radio-group>
      </el-form-item>

      <el-form-item label="盘点数量:" prop="num">
        <el-input v-model="form.num" placeholder="请输入盘点数量" class="tel_input" />
      </el-form-item>
      <el-form-item label="库存数量:" prop="kn">
        <el-input v-model="form.kn" placeholder="请输入库存数量" class="tel_input" :formatter="knToDate" />
      </el-form-item>
      <el-form-item label="盘点差额:" prop="li">
        <el-input v-model="form.li" placeholder="请输入差额" class="tel_input" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="sure">确定</el-button>
      </span>
    </template>
  </el-dialog>

  <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="40" />
    <el-table-column prop="goodsId" label="盘点编号" min-width="60" align="center" />

    <el-table-column prop="sp" label="商品名称" width="100">
      <!-- <template #default="scope">{{ scope.row.date }}</template> -->
    </el-table-column>
    <!-- <el-table-column prop="goodsId" label="商品名称" min-width="60" align="center" /> -->
    <el-table-column prop="dw" label="单位" min-width="60" align="center" />
    <el-table-column prop="num" label="盘点数量" min-width="60" align="center" />
    <el-table-column prop="kn" label="库存数量" min-width="60" align="center" />
    <el-table-column prop="li" label="盘点差额" :formatter="timeToDate" min-width="60" align="center" />
    <el-table-column label="操作" min-width="100" align="center">
      <template #default="scope">
        <el-button size="small" class="butt">差额说明</el-button>
        <el-button size="small" style="color: #fff" color="#f56c6c" @click="remove(scope.row.goodsId)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin-top: 20px"></div>
  <!-- <el-button type="primary" @click="end">盘点结束</el-button>
  <el-button type="primary" @click="clean">清空</el-button> -->
  <!-- 分页 -->
  <div class="fy">
    <el-button type="primary" @click="fy1">首页</el-button>
    <el-pagination layout="prev, pager, next" :total="total" @current-change="fn" />
    <!-- <el-pagination layout="prev, pager, next" :total="50" /> -->
    <el-button type="primary" @click="fy2">尾页</el-button>
  </div>
</template>
<script setup>
import { onMounted, ref, watch, reactive } from 'vue'
import { add1Api, dataApi, removeApi } from '@/api/api.js'

import '@/mock/data.js'
import moment from 'moment'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const total = ref(0)
const tableData = ref([])
const router = useRouter()

// 设置当前页数为1
const currentPage = ref(1)
const getData = async () => {
  const res = await dataApi({ currentPage: currentPage.value, pageSize: 10 })

  console.log(res.data)
  tableData.value = res.data.result
  total.value = res.data.total
}
//页码切换的数据
const fn = (num) => {
  currentPage.value = num
  localStorage.setItem('currentPage', currentPage.value)
}
//监听页码的改变直接做请求
watch(currentPage, () => {
  getData()
})

// 点击切换
const fy1 = () => {
  currentPage.value = 1
}
const fy2 = () => {
  fn(total.value / 10)
}

// 删除
const remove = (goodsId) => {
  ElMessageBox.confirm('你确定删除吗?', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })

    .then(() => {
      removeApi({ id: goodsId }).then((res) => {
        ElMessage({
          message: res.data.msg,
          type: 'success',
        })
        console.log(res.data.msg)
        getData()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消成功',
      })
    })
}

// 新增
const open = ref(false)
const form = reactive({
  sp: '',
  dw: '',
  num: '',
  kn: '',
  li: '',
})
const add = () => {
  open.value = true
}
const sure = async () => {
  const res = await add1Api({ add1: form })
  open.value = false

  router.push('/home/houqin/kucun/pand')
}

onMounted(() => {
  getData()
})
</script>

<style lang="less" scoped>
.pan {
  text-align: center;
  padding: 10px;
  font-size: 15px;
}
.butt {
  height: 32px;
  width: 80px;
  background-color: #ffd000a2;
  border-radius: 4px;

  color: #f5f5f5;
  border: none;
}
// 修改分页
.fy {
  width: 100%;
  height: 60px;
  background-color: #fff;

  margin-top: 14px;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-pagination {
    justify-content: center;

    padding: 6px;
    .btn-prev,
    .el-icon {
      margin-top: 8px;
    }
  }
}
</style>
